/**
 * Copyright 2008 William J. Szerdy Jr.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @author Bill Szerdy <bszerdy@gmail.com>
 *
 * 12.08.2008 - bszerdy : Created.
 *
 */

var timerID             = 0;
var tStart              = null;
var blind_length        = 0;
var blind_raw_length    = 0;
var total_seconds       = 0;

/**
 * 'Ticks' the timer 
 */
function UpdateTimer() {
	current_level = document.getElementById('current_level').value;
	highest_level = document.getElementById('highest_level').value;

	if (parseInt(current_level) > parseInt(highest_level)) {
		document.write("Tournament Complete");
		Stop();
	}
	
	if (timerID) {
		clearTimeout(timerID);
		clockID = 0;
	}

	if (!tStart)
		tStart = new Date();

	var tDate = new Date();
   	var tDiff = tDate.getTime() - tStart.getTime();

	tDate.setTime(tDiff);

	total_seconds++;
	time = document.getElementById('timer');
	
	if (GetRawSeconds(time.value) >= blind_raw_length-1) {
		document.getElementById('blindmain').submit();
	}
	
	time.value = PadDigits(tDate.getMinutes(), 2) + ":" + PadDigits(tDate.getSeconds(), 2);

	percent = parseInt((total_seconds / blind_raw_length) * 100);

	myJsProgressBarHandler.setPercentage('element1', percent);
   
	timerID = setTimeout("UpdateTimer()", 1000);
	
}

/**
 * Starts the timer
 */
function Start(lng) {
	blind_raw_length = lng;
	
	blind_length = blind_raw_length >= 60 ? (lng / 60) : lng;

	tStart = new Date();

	time = document.getElementById('timer');
	time.value = "00:00";

	timerID = setTimeout("UpdateTimer()", 1000);
}

/**
 * Stops the form timer.
 */
function Stop() {
	if (timerID) {
		clearTimeout(timerID);
		timerID = 0;
	}

	tStart = null;
}

/**
 * Resets the form by reloading the page.
 */
function Reset(where) {
	document.location   = where;
	timerID             = 0;
	blind_length        = 0;
	blind_raw_length    = 0;
	total_seconds       = 0;
	tStart              = null;

	time = document.getElementById('timer');
	time.value = "00:00";
	myJsProgressBarHandler.setPercentage('element1','0');
}

/**
 * Just a quick digit padder I found somewhere on the intertubes.
 */
function PadDigits(n, totalDigits) {
	var n = n.toString();
	var pd = '';
	
	if (totalDigits > n.length) {
		for (i=0; i < (totalDigits-n.length); i++) {
			pd += '0';
		}
	}
	return pd + n.toString();
}

/**
 * returns an integer value for a string representation of
 * the time in mm:ss format.
 */
function GetRawSeconds (timeString) {
	var time = timeString.split(":");
	
	var min = parseInt(time[0]);
	var sec = parseInt(time[1]);
	var retval = 0;

	retval += min * 60;
	retval += sec;

	return retval;
}





